package aiyh.utils.sqlUtil.whereUtil.impl;

import aiyh.utils.sqlUtil.whereUtil.InnerWhereOperator;
import aiyh.utils.sqlUtil.whereUtil.Where;
import aiyh.utils.sqlUtil.whereUtil.WhereOperator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author EBU7-dev1-ayh
 * @date 2021/8/23 0023 13:18
 * 实现类
 */


public class PrepWhereImpl implements Where {
	private final WhereOperator whereOperator;
	private final StringBuilder sqlBuilder;
	private final List<Object> args;
	private final InnerPrepWhereImpl innerWhere;
	{
		this.whereOperator = new PrepWhereOperatorImpl(this);
		this.sqlBuilder = new StringBuilder("where 1 = 1 ");
		this.args = new ArrayList<>();
		this.innerWhere = new InnerPrepWhereImpl(this);
	}

	@Override
	public WhereOperator whereAnd(String key) {
		this.sqlBuilder.append(" and ");
		this.sqlBuilder.append(key);
		return this.whereOperator;
	}

	@Override
	public WhereOperator whereOr(String key) {
		this.sqlBuilder.append(" or ");
		this.sqlBuilder.append(key);
		return this.whereOperator;
	}

	@Override
	public InnerWhereOperator andInnerWhere(String key){
		this.sqlBuilder.append(" and ( ");
		this.sqlBuilder.append(key);
		return this.innerWhere.getWhereOperator();
	}

	@Override
	public InnerWhereOperator orInnerWhere(String key){
		this.sqlBuilder.append(" or ( ");
		this.sqlBuilder.append(key);
		return this.innerWhere.getWhereOperator();
	}



	public void whereAppend(Object key){
		this.sqlBuilder.append(key);
	}

	public void addArgs(Object value){
		this.args.add(value);
	}


	@Override
	public String getSql(){
		return this.sqlBuilder.toString().replace(" 1 = 1  and", "").replace(" 1 = 1  or","");
	}

	@Override
	public List<Object> getArgs() {
		return this.args;
	}
}
